package com.hetongxue.system.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hetongxue.system.domain.Label;
import com.hetongxue.system.domain.dto.RequestPageDto;
import com.hetongxue.system.mapper.LabelMapper;
import com.hetongxue.system.service.LabelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @Description: 标签业务处理
 * @ClassNmae: LabelServiceImpl
 * @Author: 何同学
 * @DateTime: 2022-05-10 20:00
 **/
@Service
@Transactional
@RequiredArgsConstructor
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label> implements LabelService {

    private final LabelMapper labelMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Label> getLabel() {
        return labelMapper.selectList(null);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Map<Object, Object> selectPageLabel(RequestPageDto requestVo) {
        Page<Label> labelPage = null;
        // 没有任何查询参数时
        if (requestVo.getSearchDate().isEmpty() && requestVo.getSearchVal().isEmpty()) {
            labelPage = labelMapper.selectPage(new Page<>(requestVo.getCurrentPage(), requestVo.getPageSize()), null);

        }
        // 有查询值时
        if (!requestVo.getSearchVal().isEmpty()) {
            labelPage = labelMapper.selectPage((new Page<>(requestVo.getCurrentPage(), requestVo.getPageSize())),
                    new QueryWrapper<Label>().like("name", requestVo.getSearchVal()));
        }
        return MapUtil.builder()
                .put("list", labelPage.getRecords())
                .put("total", labelPage.getTotal())
                .build();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateLabel(Label label) {
        return labelMapper.updateById(label);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertLabel(Label label) {
        return labelMapper.insert(label);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteLabelById(Long id) {
        return labelMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBatchLabelByIds(Collection<Long> labelIds) {
        return labelMapper.deleteBatchIds(labelIds);
    }
}
